package com.dianzi.certification.controller;

import com.dianzi.certification.dto.CertificationDTO;
import com.dianzi.certification.entity.Certification;
import com.dianzi.certification.service.CertificationService;
import com.dianzi.certification.utils.JwtUtils;
import com.dianzi.certification.vo.CertificationVO;
import com.dianzi.certification.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.dianzi.certification.constants.CertConstant.CERTIFICATION;

/**
 * @author copa
 * @createDate 2022-03-28 21:55
 * @function 流程：认证申请
 */
@RestController
@Api(tags = {"流程：认证申请"})
public class CertificationController {

    @Resource
    private CertificationService certificationService;

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @PostMapping("cert")
    @ApiOperation(value = "添加申请")
    public R<String> addCert(@RequestBody CertificationDTO certificationDTO,
                     HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return certificationService.addCert(certificationDTO, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @GetMapping("cert")
    @ApiOperation(value = "查看申请")
    public R<CertificationVO> getCert(HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return certificationService.getCert(CERTIFICATION, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @PutMapping("cert")
    @ApiOperation(value = "编辑申请")
    public R<String> putCert(@RequestBody CertificationDTO certificationDTO,
                     HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return certificationService.putCert(certificationDTO, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @GetMapping("cert/{certId}")
    @ApiOperation(value = "查看申请（详细）")
    public R<Certification> getCertDetail(@PathVariable Integer certId, HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return certificationService.getCertDetail(certId, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @GetMapping("admin/certlist")
    @ApiOperation(value = "查看认证列表（认证专家）")
    public R<List<Certification>> certList() {
        return certificationService.certList();
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @GetMapping("admin/certDetail/{certId}")
    @ApiOperation(value = "查看详细认证（认证专家）")
    public R<Certification> getCertDetailAdmin(@PathVariable Integer certId) {
        return certificationService.getCertDetailAdmin(certId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @PutMapping("admin/cert")
    @ApiOperation(value = "判定认证是否通过：第一阶段")
    public R<String> putCertAdmin(@RequestParam @ApiParam("认证id") Integer certId,
                                         @RequestParam @ApiParam("同意-1 驳回-2") Integer status,
                                         @RequestParam(required = false) @ApiParam("若驳回写理由") String remark) {
        return certificationService.putCertAdmin(certId, status, remark, CERTIFICATION);
    }
}
